/*
 * $$Id: jforumStartUp.java,v 1.3 2004/06/09 14:24:03 bel70 Exp $$
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License
 * at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and
 * limitations under the License.
 *
 * The Original Code is jforum forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *              Dmitry Belov <bel@jresearch.org>
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Created on 19.05.2004
 * 
 */
package com.jred.jforum.plugin;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import com.jred.jforum.IConst;
import com.jred.jforum.configuration.Configurator;
//import org.jresearch.gossip.mail.MailProcessor;
//import org.jresearch.gossip.mail.MailQueue;
//import org.jresearch.gossip.util.MessageProcessor;
/**
 * @author Bel
 * 
 */
public class JForumStartUp implements PlugIn, IConst {
	private static Log log = LogFactory.getLog(JForumStartUp.class);
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.PlugIn#destroy()
	 */
	public void destroy() {
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.PlugIn#init(org.apache.struts.action.ActionServlet,
	 *      org.apache.struts.config.ModuleConfig)
	 */
	public void init(ActionServlet servlet, ModuleConfig moduleconfig)
			throws ServletException {
		if (log.isInfoEnabled()) {
			log.info("init is started");
		}
		ServletContext app = servlet.getServletContext();
		app.setAttribute(IConst.CONTEXT.LAST_UPDATE_DATE, new Date());
//		app.setAttribute(IConst.CONTEXT.MAIL_QUEUE, new MailQueue(
//				new MailProcessor()));
//		try {
//            setupDataSource(app);
//        } catch (IOException e) {
//            throw new ServletException(e);
//        } catch (Exception e) {
//            throw new ServletException(e);
//        }
		//Setting up MessageProcessor
//		MessageProcessor.setEmoticonsMap(ResourceBundle
//				.getBundle("org/jresearch/gossip/resources/emoticon"));
		//Load configuration parameters
		Configurator conf = Configurator.getInstance();
//		conf
//				.setDataSource((DataSource) app
//						.getAttribute(IConst.CONTEXT.DS_KEY));
		try {
			conf.load(app);
		} catch (SQLException e) {
			if (log.isErrorEnabled()) {
				log.error("jforum Configurator is not loaded");
				e.printStackTrace();
			}
			throw new ServletException(e);
		} catch (IOException e) {
			if (log.isErrorEnabled()) {
				log.error("jforum Configurator is not loaded");
			}
			throw new ServletException(e);
		}
	}
	// Create DataSource and store it in application context
//	private void setupDataSource(ServletContext app) throws IOException,Exception {
//	    Properties dbconf=new Properties();
//	    InputStream in=app.getResourceAsStream("/WEB-INF/classes/org/jresearch/gossip/resources/db.properties");
//	    dbconf.load(in);
//		DataSource ds = BasicDataSourceFactory.createDataSource(dbconf);
//		app.setAttribute(IConst.CONTEXT.DS_KEY, ds);
//	}
}